package com.foreknow.controller;

import com.foreknow.model.Gusetbook;
import com.foreknow.service.GuestbookeService;
import com.foreknow.service.impl.GusetbookeServiceInpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


@WebServlet(name = "GetMessageServlet")
public class GetMessageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

      response.setContentType("text/html;charset=utf-8");
      request.setCharacterEncoding("utf-8");
      PrintWriter out = response.getWriter();
      //调用业务层查询所有留言信息的方法
      GuestbookeService service = new GusetbookeServiceInpl();
      //这里的方法返回值是一个集合,所以要把信息拿到要进行遍历
      List<Object> list = service.quertInfo();
      out.println("list中元素的个数:"+list.size()+"<br>");
      out.println("<html>");
      out.println("<head><title>get message</title></head>");
      out.println("<body>");
      out.println("<table>");
      out.println("<tr>");
      out.println("<td>id</td>");
      out.println("<td>name</td>");
      out.println("<td>phone</td>");
      out.println("<td>email</td>");
      out.println("<td>title</td>");
      out.println("<td>content</td>");
      out.println("</tr>");

      for(int i = 0;i<list.size();i++){
        //从集合中获取Guestbook对象,获取到的对象要进行强制类型转换到你想要的类型,因为list集合的类型是obejct
        Gusetbook gb = (Gusetbook) list.get(i);
        out.println("<tr>");
        out.println("<td>"+gb.getId()+"</td>");
        out.println("<td>"+gb.getName()+"</td>");
        out.println("<td>"+gb.getPhone()+"</td>");
        out.println("<td>"+gb.getEmail()+"</td>");
        out.println("<td>"+gb.getTitle()+"</td>");
        out.println("<td>"+gb.getContent()+"</td>");
        out.println("</tr>");
      }


      out.println("</table>");
      out.println("</body>");
      out.println("</html>");

    }
    }

